<template>
	<view>
		<view class="goods-list">
		  <view v-for="(item,index) in goodslist" :key="index" @click="itemId(item)">
      <my-goods :item="item"></my-goods>
      </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				quetyObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodslist:[],
        total:0,
       isloading: false
			};
		},
    onLoad(options){
      this.quetyObj.query = options.query || ''
      this.quetyObj.cid = options.cid || ''
      this.goodsListIndex()
    },
    methods:{
     async goodsListIndex(cb){
      this.isloading = true
      const {data:res} =  await uni.$http.get('/api/public/v1/goods/search',this.quetyObj)
       this.isloading = false
       cb && cb()
      if(res.meta.status !== 200) return uni.$showMsg()
       this.goodslist = [...this.goodslist,...res.message.goods]
       this.total = res.message.total
      },
      itemId(item){
        uni.navigateTo({
          url:'/subpkg/goods_detrail/goods_detrail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom(){
      if (this.quetyObj.pagenum * this.quetyObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      if (this.isloading) return
      this.quetyObj.pagenum++
      this.goodsListIndex()
    },
    onPullDownRefresh() {
      // 1. 重置关键数据
      this.quetyObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
    
      // 2. 重新发起请求
      this.goodsListIndex(() => uni.stopPullDownRefresh())
    }
	}
</script>

<style lang="scss">

</style>
